﻿using System.Diagnostics;
using System.Text;
using devtm.Aop.InternalProcess.Decompiling;
using System;
using System.Reflection;

namespace devtm.Aop.Event
{

    public static class InterceptEventArgsHelper
    {

        public const string BaseExtendedName = "__ExT__eNded_";
        public const string MaskBaseExtendedName = "{0}__ExT__eNded_";



        public static string GetCode(this InterceptEventArgs args, bool html, out int start, out int end, out int line)
        {
            StackTrace s = args.Stack as StackTrace;
            return s.GetCode(html, out start, out end, out line);
        }


        public static string SerializeStack(this InterceptEventArgs args)
        {

            StringBuilder t = new StringBuilder();


            t.Append(args.Member.ToString() + " Parameters : (");
            SerializeParameters(t, args, ", ");
            t.AppendLine(")");

            StackTrace st = new StackTrace(5);
            StackFrame[] fs2 = st.GetFrames();
            StackFrame[] fs = args.Stack.GetFrames();

            for (int i = 0; i < fs2.Length - 1; i++)
            {

                var s = fs2[i];
                string n = s.GetMethod().ToString();
                t.AppendLine(n);
            }

            return t.ToString();

        }


        public static string SerializeParameters(this StringBuilder s, Event.InterceptEventArgs args, string splitChar)
        {

            bool t1 = false;

            foreach (var item in args.parameters)
            {

                if (t1)
                    s.Append(splitChar);

                if (item == null)
                    s.Append("null");

                else
                {
                    Type t = item.GetType();
                    if (item is string)
                        s.Append(@"""" + (item as string) + @"""");

                    else if (t.IsClass)
                        s.Append(item.GetHashCode());

                    else if (t.IsEnum)
                        s.Append(t.FullName + "." + item.ToString());

                    else
                        s.Append(item.ToString());
                }
                t1 = true;

            }
            return s.ToString();
        }




        [DebuggerStepThrough]
        public static string SerializeParameters(this StringBuilder s, Event.InterceptEventArgs args)
        {


            foreach (var item in args.parameters)
            {


                if (item == null)
                    s.Append("null");

                else
                {

                    Type t = item.GetType();

                    if (item is string)
                        s.Append(item as string);

                    else if (t.IsClass)
                        s.Append(item.GetHashCode());

                    else if (t.IsEnum)
                        s.Append((int)item);

                    else
                        s.Append(item.ToString());
                }

            }
            return s.ToString();
        }


    }

}


